package com.weex.app.module.signature;


import android.graphics.Bitmap;
import android.widget.Toast;

import com.abe.signaturesdk.OnSignatureCallBack;
import com.abe.signaturesdk.SignatureDialog;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;
import com.weex.app.module.tool.ImageUtils;
import com.weex.app.util.LocalLogUtils;
import com.weex.app.util.PermissionUtils;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.Permission;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SignatureModule extends WXModule {

    @JSMethod
    public void showSign(final JSCallback callback) {
        PermissionUtils.createRequest(mWXSDKInstance.getContext(), new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                //展示签名弹窗
                SignatureDialog dialog = new SignatureDialog(mWXSDKInstance.getContext(), String.valueOf(System.currentTimeMillis()));
                dialog.setCallBackListener(new OnSignatureCallBack() {
                    @Override
                    public void onSignEnd(final Bitmap bitmap, String filePath) {
                        //数据处理位置
                        Map<String, Object> map = new HashMap<>();
                        map.put("imgPath", filePath);
                        map.put("base64Img", ImageUtils.bitmapToString(bitmap));
                        callback.invokeAndKeepAlive(map);
                    }
                });
                dialog.show();
                dialog.setDialogWindowAttr(mWXSDKInstance.getContext(), false);
            }
        }, Permission.Group.STORAGE).start();
    }

    @JSMethod
    public void bitmapToString(Bitmap bitmap, JSCallback callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("base64Img", ImageUtils.bitmapToString(bitmap));
        callback.invokeAndKeepAlive(map);
    }

    @JSMethod
    public void stringToBitmap(String imgString, JSCallback callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("bitmap", ImageUtils.stringToBitmap(imgString));
        callback.invokeAndKeepAlive(map);
    }

    @JSMethod
    public void printLog(String str) {
        Toast.makeText(mWXSDKInstance.getContext(), str, Toast.LENGTH_SHORT).show();
    }

    @JSMethod
    public void localLog(String error) {
        LocalLogUtils.saveInfo2File(String.valueOf(System.currentTimeMillis()), error);
    }
}
